<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
  <el-container>
      <!-- 侧边栏 -->
    <el-aside :width="isCollapse? '64px':'200px'">
      <div class="toggle-button" @click="toggleCollapse">|||</div>
      <!-- unigue-opened:只保持一个子菜单打开 -->
    <el-menu background-color="#333744"
text-color="#fff"
active-text-color="blue"
unique-opened
:collapse="isCollapse"
:collapse-transition="false"
:default-active="activepath"
:router="true">
    <!-- router代表菜单自带跳转功能 -->
      <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
        <template #title>
          <i :class="iconsObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item :index="'/'+children.path" v-for="children in item.children" :key="children.id" @click="SaveNavState('/'+children.path)">
          <i class="el-icon-menu"></i>
          <span>{{children.authName}}</span>
        </el-menu-item>
      </el-submenu>
    </el-menu>
      </el-aside>
      <!-- 正文区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-lock',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      isCollapse: false,
      activepath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activepath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      //  退出功能
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res)
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    SaveNavState(activepath) {
      window.sessionStorage.setItem('activePath', activepath)
      this.activepath = activepath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display:flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  >div{
    display: flex;
    align-items: center;//元素垂直居中
    span{
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
.toggle-button{
  background-color: #1e1e20;
  line-height: 24px;
  font-size: 10px;
  color: #ffff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-main {
  background-color: #eaedf1;
}
</style>
